package com.javachmapi.jdbc;

import com.javachmapi.domain.Account;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import org.springframework.stereotype.Component;

import java.beans.PropertyVetoException;
import java.sql.*;
import java.util.List;

/**
 * @Description： create by 宋利军
 * 2020\5\30 003010:32
 */
@Component
public class MyJdbcImpl {

    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    private QueryRunner runner=new QueryRunner(dataSource);

    public List<Account> findAllUser() {
        try {
            dataSource = new ComboPooledDataSource();
            dataSource.setUser("root");
            dataSource.setPassword("s742503");
            dataSource.setJdbcUrl("jdbc:mysql://106.14.117.234:3306/javachmapi");
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        // 实现查询所有员工列表
        try {
            return runner.query("select * from account", new BeanListHandler<Account>(Account.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
